package crp.services.api.graphql;

import crp.core.space.app.SpaceService;
import crp.services.api.graphql.models.LocationPayload;
import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Query;

import javax.annotation.security.PermitAll;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

@GraphQLApi
@RequestScoped
@Transactional
public class LocationGraphQLApi {
    private final SpaceService spaceService;

    @Inject
    public LocationGraphQLApi(SpaceService spaceService) {
        this.spaceService = spaceService;
    }

    @Query
    @PermitAll
    @NotNull
    public List<@NotNull LocationPayload> rootLocations() {
        return spaceService.locationSearcher().root()
                .map(LocationPayload::of)
                .collect(Collectors.toList());
    }
}